/**
 * Provides iteration over an arrays or objects in JavaScript.
 *
 * @author Andrey Anisimov <hercules@inbox.ru>
 */

class Iterator {
	
	/**
	 * Initializes the new iterator instance.
	 * 
	 * @param collection (array|object) The collection to iterate over.
	 */
	this.Iterator = function(collection) {
		this.index = 0;
		if (collection instanceof Object) {
			this.collection = [];
			for (var k in collection) {
				this.collection.push(collection[k]);
			}
		} else {
			this.collection = collection;
		}
	}
	
	/**
	 * Checks whether the iteration has more elements.
	 *
	 * @return bool
	 */
	this.hasNext = function() {
		return (this.index < this.collection.length);
	}
	
	/**
	 * Returns the next element in the iteration.
	 * 
	 * @throws Error The iteration has no more elements
	 * @return variant
	 */
	this.next = function() {
		if (!this.hasNext()) {
			throw new Error('The iteration has no more elements');
		} else {
			return this.collection[this.index++];
		}
	}
}